package algorithm.guess;

import sheffield.EasyReader;
import sheffield.EasyWriter;

public class UserGuesses extends BaseGame {

    private String actualWord = "";

    private EasyReader keyboard;

    public UserGuesses(EasyReader k, EasyWriter s) {
        super(k, s);
        this.keyboard = k;
        // 随机选则一个单词
    }

    @Override
    public void run() {
        int attempt = 1;
        int maxAttempt = 6;
        char[] availableLetters = {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
        };
        String[] tryedWord = new String[]{};
        String gameInstructions = "Constructing a new game with user guesses\n" +
                "The computer has selected a word, and you have 6 attempts to algorithm.guess it. \n" +
                "The computer will respond with:\n" +
                "+ to denote a correct character in the right location.\n" +
                "* to denote a correct character.\n" +
                "X to denote an incorrect character.";
        System.out.println(gameInstructions);

        for (int i = 1; i <= 6; i++) {
            String newWord = keyboard.readString("Please enter a word, 5 characters long :");
            int contains = contains(tryedWord, newWord);
            if (contains >= 0) {
                //如果使用尝试过 单独提示
                System.out.printf("Attempt %d of 6", contains + 1);
            }else{
                newWord = keyboard.readString("Please enter a word, 5 characters long :");
            }
            System.out.printf("Available letters : abcdefghijklmnopqrstuvwxyz", i);
            Checking - idler - is valid
            Word - idler - contains a - z only
            Word - idler - is valid
            Your guess :idler
            Response:
            XX +*X
        }
    }

    public String[] insertElement(String[] array, String element) {
        // 创建一个新的数组，长度为原数组长度加1
        String[] newArray = new String[array.length + 1];
        for (int i = 0; i < array.length; i++) {
            newArray[i] = array[i]; // 复制原数组中插入位置之前的元素
        }
        newArray[array.length] = element; // 插入新元素
        return newArray;
    }


    private int contains(String[] array, String element) {
        //数组中是否包含指定元素
        for (int i = 0; i < array.length; i++) {
            if (element.equals(array)) {
                return i;
            }
        }
        return -1;
    }

    private static String getFeedback(String selectedWord, String userGuess) {
        StringBuilder feedback = new StringBuilder();
        boolean[] guessedCorrectly = new boolean[selectedWord.length()];

        // First pass: check for exact matches (correct character in the right location)
        for (int i = 0; i < selectedWord.length(); i++) {
            if (userGuess.charAt(i) == selectedWord.charAt(i)) {
                feedback.append('+');
                guessedCorrectly[i] = true;
            } else {
                feedback.append('X');
            }
        }

        // Second pass: check for correct characters in the wrong location
        for (int i = 0; i < selectedWord.length(); i++) {
            if (!guessedCorrectly[i] && selectedWord.contains(String.valueOf(userGuess.charAt(i)))) {
                feedback.setCharAt(i, '*');
            }
        }

        return feedback.toString();
    }

}
